'use strict';
function rem(config) {
	var defaultConfig = {
		isScale: true,
		hopeFontSize: 40,
		designPage: 750,
	};
	config = config || defaultConfig;
	var MAXWIDTH = 540,
		MINWIDTH = 320;
	var dpr = window.devicePixelRatio || 1,
		htmlEl = document.documentElement,
		viewportEl = document.querySelector('meta[name=viewport]');
	if (config.boundaryCallBack) {
		var temDpr = null;
		temDpr = config.boundaryCallBack();
		dpr = parseInt(temDpr);
	} else {
		dpr = dpr > 3 ? 3 : dpr > 2 ? 2 : 1;
	}
	var scale = config.isScale ? 1 / dpr : 1;
	var content =
		'width=device-width,initial-scale=' +
		scale +
		',user-scalable=no,maximum-scale=' +
		scale +
		',minimum-scale=' +
		scale;
	if (viewportEl) {
		viewportEl.setAttribute('content', content);
	} else {
		viewportEl = document.createElement('meta');
		viewportEl.setAttribute('name', 'viewport');
		viewportEl.setAttribute('content', content);
		htmlEl.appendChild(viewportEl);
	}
	htmlEl.setAttribute('data-dpr', dpr);
	htmlEl.setAttribute('MaxWidth', MAXWIDTH);
	htmlEl.setAttribute('MinWidth', MINWIDTH);
	function setRem() {
		var RADIO = config.designPage / config.hopeFontSize;
		var viewportWidth =
			window.innerWidth ||
			document.documentElement.clientWidth ||
			document.documentElement.getBoundingClientRect().width;
		if (MAXWIDTH && viewportWidth / dpr > MAXWIDTH) {
			viewportWidth = MAXWIDTH * dpr;
		} else if (MINWIDTH && viewportWidth / dpr < MINWIDTH) {
			viewportWidth = MINWIDTH * dpr;
		}
		htmlEl.style.fontSize = viewportWidth / RADIO + 'px';
	}
	window.addEventListener('resize', setRem);
	setRem();
}
